package hezi.helper;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import dz.common.DBHelper;

public class DBKiller {
	public static Logger logger = Logger.getLogger("");
	static String newdb = "C:\\forums2.db";

	public static void main(String[] args) throws SQLException {
		Map<String,String> idandstoredposturl = new HashMap<String,String>();

		Connection conn = DBHelper.getConnection(newdb);
		String sql = "select * from post where siteid = 'av'";
		Statement stat = conn.createStatement();
		ResultSet rs = stat.executeQuery(sql);
		int count = 0;
		while(rs.next()){
			count++;
			String url = rs.getString("storedposturl");
			int index = url.indexOf("&");
			if(index == -1){
				logger.info("badurl:" + url);
				continue;
			}
			url = url.substring(0,index);
			idandstoredposturl.put(rs.getString("postid"), url);
			if(count%100 ==0 ){
				logger.debug(count + " " + rs.getString("postid") + " " + url);
			}
		}
		rs.close();
		stat.close();
		
		//
		Statement stat2 = conn.createStatement();
		int count2=0;
		for(String id : idandstoredposturl.keySet()){
			String url = idandstoredposturl.get(id);
			stat2.executeUpdate( "UPDATE post SET storedposturl ='" + url +"' WHERE postid='" + id+"'");
			if(count2++%100 ==0 ){
				logger.debug(count + " done ");
			}
		}
		
		
		
		
		

	}

}
